package com.econ.powercloud.ems.util;

import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @ Description:
 * @ Author：hgp
 * @ Create:2022-05-30-10:59
 */
public class TransformUtil {
    private TransformUtil() {
    }

    public static <T> Map<String, String> tramsform( T t, Map<String, String> map) {
        Field[] fields = t.getClass().getDeclaredFields();
        Field.setAccessible(fields, true);
        Map<String, String> result = new HashMap<>();
        for (Field field : fields) {
            String value = null;
            try {
                Object val=  field.get(t);
                if(val!=null){
                    value=val.toString();
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            if (map.containsKey(field.getName()) && !StringUtils.isEmpty(value)) {
                result.put(map.get(field.getName()), value);
            }
        }
        return result;
    }
}
